#ifndef __SINGLETON_H__
#define __SINGLETON_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <cassert>
#include "MySingleLock.h"
#include <memory>

template<typename T>
class Singleton
{
private:
	static std::auto_ptr<T> s_pSingleton;
	static CMyCriticalSection m_lock;

public:
	Singleton(void);
	virtual ~Singleton();
public:
	static T *GetInst(void);
	static void Release(void);
};

template<typename T>
inline Singleton<T>::Singleton(void)
{
}

template<typename T>
inline Singleton<T>::~Singleton(void)
{
}

template<typename T>
inline T *Singleton<T>::GetInst(void)
{
	if (!s_pSingleton.get()) 
	{
		m_lock.Lock();
		if (!s_pSingleton.get())
		{
			s_pSingleton.reset(new T);
			//s_pSingleton = std::auto_ptr<T>(new T);
		}
		m_lock.Unlock();
	}
	return s_pSingleton.get();
}


template<typename T>
inline void Singleton<T>::Release(void)
{
	if (s_pSingleton.get())
	{
		m_lock.Lock();
		delete s_pSingleton.release();
		//delete s_pSingleton.reset();
		m_lock.Unlock();
	}
}

template<typename T>
std::auto_ptr<T> Singleton<T>::s_pSingleton;
template<typename T>
CMyCriticalSection Singleton<T>::m_lock;

#endif